// 简易打怪游戏逻辑 - 虚假钱包版本
new Vue({
    el: "#app",
    data() {
        return {
            // 虚假钱包状态
            walletConnected: false,
            walletAddress: "",

            // 游戏数据
            tokenBalance: "0",
            hero: null,
            heroName: "",
            creatingHero: false,

            // 怪物数据
            monsters: [
                { name: "小怪兽", power: 30, reward: 100 },
                { name: "中怪兽", power: 60, reward: 300 },
                { name: "大怪兽", power: 90, reward: 800 },
            ],

            // 战斗状态
            fighting: false,
            battleResultVisible: false,
            battleResult: {},
        }
    },
    methods: {
        // 虚假连接钱包
        connectWallet() {
            if (this.walletConnected) return

            // 生成随机虚假地址
            const randomHex = "0x" + Math.random().toString(16).substr(2, 40)
            this.walletAddress = randomHex
            this.walletConnected = true

            // 初始代币余额
            this.tokenBalance = "1000"

            this.$message.success("钱包连接成功")
        },

        // 断开钱包
        disconnectWallet() {
            this.walletConnected = false
            this.walletAddress = ""
            this.hero = null
            this.$message.info("钱包已断开")
        },

        // 格式化显示地址
        shortAddress(address) {
            if (!address) return ""
            return address.slice(0, 6) + "..." + address.slice(-4)
        },

        // 创建英雄
        createHero() {
            if (!this.walletConnected) {
                this.$message.warning("请先连接钱包")
                return
            }

            if (this.hero) {
                this.$message.info("你已经有一个英雄了")
                return
            }

            const name = this.heroName.trim()
            if (!name) {
                this.$message.error("请输入英雄名称")
                return
            }

            this.creatingHero = true

            // 模拟创建延迟
            setTimeout(() => {
                this.hero = {
                    name: name,
                    power: Math.floor(Math.random() * 100) + 1,
                    wins: 0,
                }

                this.heroName = ""
                this.creatingHero = false
                this.$message.success(
                    `英雄 ${name} 创建成功！战斗力: ${this.hero.power}`
                )
            }, 800)
        },

        // 获取怪物图标
        getMonsterIcon(index) {
            const icons = ["el-icon-eleme", "el-icon-ship", "el-icon-fire"]
            return icons[index] || "el-icon-warning"
        },

        // 挑战怪物
        fightMonster(monsterIndex) {
            if (!this.hero) {
                this.$message.warning("请先创建英雄")
                return
            }

            if (this.fighting) return

            const monster = this.monsters[monsterIndex]
            this.fighting = true

            // 模拟战斗过程
            setTimeout(() => {
                // 战斗逻辑：英雄战斗力 + 随机值 > 怪物战斗力
                const randomFactor = Math.floor(Math.random() * 21)
                const won = this.hero.power + randomFactor > monster.power

                // 更新英雄属性
                let newPower = this.hero.power
                if (won) {
                    newPower += 2
                    this.hero.wins += 1
                    this.tokenBalance = (
                        parseInt(this.tokenBalance) + monster.reward
                    ).toString()
                } else {
                    newPower = newPower > 2 ? newPower - 2 : 1
                }
                this.hero.power = newPower

                // 战斗结果
                this.battleResult = {
                    won: won,
                    monsterName: monster.name,
                    reward: won ? monster.reward : 0,
                    newPower: newPower,
                }

                this.battleResultVisible = true
                this.fighting = false
            }, 1200)
        },

        // 关闭战斗结果弹窗
        closeResult() {
            this.battleResultVisible = false
        },
    },
})
